package Traductor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class TablaSimbolos {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private class Entry{
		String id;
		Propiedades ps;
		public Entry (String id, Propiedades ps){
			this.id = id;
			this.ps = ps;
		}
		
		public Entry clone(){
			return new Entry(id, ps.clone());
		}
	}
	
	private List<Entry> tabla;
	
	public TablaSimbolos(){
		tabla = new ArrayList<Entry>();
	}
	
	public void put(String id, Propiedades ps){
		this.tabla.add( new Entry(id, ps) );
	}
	
	public Propiedades get(String id){
		List<Propiedades> lPs = new ArrayList<Propiedades>();
		Iterator<Entry> it = tabla.iterator();
		while( it.hasNext() ){
			Entry e = it.next();
			if( e.id.equals(id) ) lPs.add(e.ps);
		}
		Propiedades retVal;
		if( lPs.size()>0 ) retVal = lPs.get(0);
		else return null;
		Iterator<Propiedades> it2 = lPs.iterator();
		while( it2.hasNext() ){
			Propiedades p = it2.next();
			if( p.nivel > retVal.nivel ) retVal = p;
		}
		return retVal;
	}
	
	public TablaSimbolos clone(){
		TablaSimbolos ts = new TablaSimbolos();
		Iterator<Entry> it = tabla.iterator();
		while(it.hasNext())
			ts.tabla.add(it.next().clone());
		return ts;
	}
	
	public String toString(){
		String retVal = "<";
		Iterator<Entry> it = tabla.iterator();
		while (it.hasNext()){
			Entry e = it.next();
			retVal += " " + e.id + "=" + e.ps;
		}
		retVal+=">";
		return retVal;
	}
	
//	public TablaSimbolos clone(){
//		TablaSimbolos ts = new TablaSimbolos();
//		Set<String> set = this.keySet();
//		Iterator<String> it = set.iterator();
//		while (it.hasNext()){
//			String id = it.next();
//			ts.put(id, this.get(id).clone());
//		}
//		return ts;
//	}
	
	
}
